#ARGV 0:	directory of log files
#ARGV 1:	number of considered records
 
opendir $dir, $ARGV[0] or die "can not open the directory $ARGV[0]!\n";

@time;
@rssi;
$recordNum = 300000;
if($ARGV[1]!=0){
$recordNum = $ARGV[1];
}

for($i=0;$i<16;$i++){
	for($j=0;$j<$recordNum;$j++){
		$time[$i][$j] = 0;
		$rssi[$i][$j] = -150;
	}
}
$fileNum = 0;

#get the minimum start time of 16 channels.
while ($file = readdir $dir) {
	$file_path = "$ARGV[0]\\$file";
	print ("$file_path  \n $file\n");
	
	$logfile = "\.log";
	#get input samples
	if($file =~ /$logfile/) {
		GETINPUT:{
		
			open fin, "<$file_path" or die "can not open file $file_path!\n";
			$fileNum++;
			$sampleNum = 0;
			$channel = 0;
		
			$alreadyNum = 0;	
			while (<fin>) {
				chomp;
				@info = split/,/;
				
				if($info[0] eq "infoc") {
					if($channel==0){
						$channel = $info[1];
					}
					next;
				}
				
				if($info[0] eq "infos"){
					next;
				}
				
				
				if($info[0] ne "infos" && $info[0] ne "infoc"){
					$time[$channel-11][$sampleNum] = $info[0];
					$rssi[$channel-11][$sampleNum] = $info[1];
					#print "$channel-11 $sampleNum $time[$channel-11][$sampleNum] $rssi[$channel-11][$sampleNum]\n";
					#print "$info[0] $info[1]\n";
					$sampleNum++;
					if($info[0] ne "infoe"){
						$alreadyNum++;
						
					}
				}
				if($alreadyNum==$recordNum){
					print "$alreadyNum\n";
					last GETINPUT;
				}
			}
			print "$channel $sampleNum\n";
			close fin;
		}
	}
	else {
		print("No log files found!\n");
		next;
	}
}

@index[16];
for($i=0;$i<16;$i++){
	$index[$i] = 1;
}

open fout, ">$ARGV[0]\\totalSync.info" or die "can not open file $dir\\totalSync.info!\n";
MERGE: {for($j=1;$j<$recordNum;$j++){
	$stdTime = $time[0][$j];
	#print "$stdTime ";
	if($stdTime eq 0 && $stdTime ne "infoe"){
		last MERGE;
	}
	
	#one round ends
	if($stdTime eq "infoe") {
		print "$stdTime !!!";
		for($i=0;$i<16;$i++){
			if(($time[$i][$index[$i]]-$time[0][$j-1])<10000){
				while($time[$i][$index[$i]] ne "infoe" && $time[$i][$index[$i]] ne 0) {
					$index[$i] += 1;
				}
				print "$i $time[$i][$index[$i]] ";
				$index[$i] += 1;
				print "$time[$i][$index[$i]]\n";
			}
		}
		next;
	}
	#if($time[0][$j+1] eq 0 && $time[0][$j+1] ne "infoe"){
	#	last MERGE;
	#}
	
	$counter= 0;
	print fout "$stdTime ";
	#print "$stdTime ";
	
	for($i=0;$i<16;$i++){
		$prntRSSI = -150;
		$found = 1;
		if($time[$i][$index[$i]]<=$stdTime){
			FIND: {while($time[$i][$index[$i]]!=$stdTime && $time[$i][$index[$i]]!=($stdTime-1) && $time[$i][$index[$i]]<=$stdTime) {
				$counter++;
				if($counter>500){
					print "$i Fuck $counter!\n";
				}
				if($rssi[$i][$index[$i]] != -150 && $time[$i][$index[$i]] ne "infoe"){
					$index[$i] += 1;
				}
				else {
					$found = 0;
					last FIND;
				}
			}
			}
			$counter = 0;
			if($time[$i][$index[$i]]>$stdTime){
				$found = 0;
			}
			
		}
		else {
			$found = 0;
		}
		
		if($found==1){
			$prntRSSI = $rssi[$i][$index[$i]];
		}
		print fout "$prntRSSI ";
	}
	print fout "\n";
}
}
close fout;


	
	
	